package com.se.mapper;

import com.se.domain.mysql.ActivityDO;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface ActMapper {
    List<ActivityDO> listByActIdList(@Param("list") List<Long> actIds);

    @Insert("insert into activity(user_id, cat_ids, title, info, place, start_time, end_time, apply_time, apply_end_time, qn_json, img_ids) " +
            "values (#{userId}, #{catIds}, #{title}, #{info}, #{place}, #{startTime}, #{endTime}, #{applyTime}, #{applyEndTime}, #{qnJson}, #{imgIds})")
    @Options(useGeneratedKeys = true, keyProperty = "actId", keyColumn = "act_id")
    void insertActivity(ActivityDO activityDO);

    @Select("select * from activity where act_id = #{actId} and active = true")
    ActivityDO getByActId(@Param("actId") Long actId);

    @Update("update activity set " +
            "cat_ids = #{catIds}, title = #{title}, info = #{info}, place = #{place}, " +
            "start_time = #{startTime}, apply_time = #{applyTime}, apply_end_time = #{applyEndTime}, qn_json = #{qnJson}, qn_id = #{qnId}, " +
            "img_ids = #{imgIds} " +
            "where act_id = #{actId}")
    void updateAct(ActivityDO activityDO);

    @Update("update activity set publish = #{publish} where act_id = #{actId} and active = true")
    void updatePublishByActId(@Param("actId") Long actId, @Param("publish") Boolean publish);

    @Update("update activity set active = false where act_id = #{actId}")
    void logicDeleteByActId(@Param("actId") Long actId);

    @Update("update activity set qn_id = #{qnId} where act_id = #{actId} and active = true")
    void updateQnIdByActId(@Param("qnId") Long qnId, @Param("actId") Long actId);

    @Select("select * from activity where act_id = #{actId} and active = true for update")
    ActivityDO selectForUpdate(@Param("actId") Long actId);


    void incrementApplyNum(@Param("actId") Long actId);


    void decrementApplyNum(@Param("actId") Long actId);


    List<ActivityDO> listByUserId(@Param("userId") Long userId, @Param("order") String order, @Param("desc") Boolean desc, @Param("l1") int l1, @Param("l2") int l2);

    List<ActivityDO> listByQnIds(@Param("list") List<Long> qnIdList, @Param("order") String order, @Param("desc") Boolean desc, @Param("l1") int l1, @Param("l2") int l2);

    @Select("select * from activity where qn_id = #{qnId} and active = true")
    ActivityDO getByQnId(@Param("qnId") Long qnId);

    @Update("update activity set publish = false, qn_id = NULL, apply_num = 0 where act_id = #{actId} and active = true")
    void cancelPublishByActId(@Param("actId") Long actId);
}
